package com.galaxy.service.config;

import com.galaxy.commom.base.filter.JwtFilter;
import com.galaxy.service.realm.MyRealm;
import com.galaxy.service.service.global.ShiroService;
import com.galaxy.service.shiro.CredentialsMatcher;
import com.galaxy.service.shiro.SubjectFactory;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.Filter;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.mgt.DefaultSessionStorageEvaluator;
import org.apache.shiro.mgt.DefaultSubjectDAO;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.crazycake.shiro.RedisCacheManager;
import org.crazycake.shiro.RedisManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.MethodInvokingFactoryBean;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * shiro配置
 * @author caijinbang
 * @date 2018/9/12 19:40
 */
@Slf4j
@Configuration
public class ShiroConfig {



  @Bean
  public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager){
    log.info("Shiro Configuration initialized");
    ShiroFilterFactoryBean shiroFilterFactoryBean  = new ShiroFilterFactoryBean();

    //设置SecurityManager
    shiroFilterFactoryBean.setSecurityManager(securityManager);
    //拦截器
    //<!-- 过滤链定义，从上向下顺序执行，一般将 /**放在最为下边 -->:这是一个坑呢，一不小心代码就不好使了;
    //<!-- authc:所有url都必须认证通过才可以访问; anon:所有url都都可以匿名访问-->
    //Map<String,String> filterChainDefinitionMap = shiroService.getFilterChainDefinitionMap();

    //过滤器
    Map<String,Filter> filters = new HashMap<>();
    filters.put("perms",new JwtFilter());
    shiroFilterFactoryBean.setFilters(filters);
    //shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
    return shiroFilterFactoryBean;
  }

  @Bean(name = "myRealm")
  public MyRealm myRealm(){
    MyRealm myRealm = new MyRealm();
    myRealm.setCredentialsMatcher(new CredentialsMatcher());
    myRealm.setAuthorizationCacheName(MyRealm.class.getName()+".authorizationCache");
    return myRealm;
  }

  @Bean(name = "securityManager")
  public SecurityManager securityManager(RedisCacheManager RedisCacheManager,@Qualifier("myRealm")MyRealm myRealm){
    DefaultWebSecurityManager manager =  new DefaultWebSecurityManager();
    manager.setRealm(myRealm());
    manager.setCacheManager(RedisCacheManager);
    ((DefaultSessionStorageEvaluator) ((DefaultSubjectDAO)manager.getSubjectDAO())
        .getSessionStorageEvaluator()).setSessionStorageEnabled(false);
    manager.setSubjectFactory(new SubjectFactory());
    return manager;
  }

  //开启shiro aop注解支持
  @Bean
  public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
    AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
    authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
    return authorizationAttributeSourceAdvisor;
  }

  /**
   * DefaultAdvisorAutoProxyCreator，Spring的一个bean，由Advisor决定对哪些类的方法进行AOP代理。
   */
  @Bean
  public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
    DefaultAdvisorAutoProxyCreator defaultAAP = new DefaultAdvisorAutoProxyCreator();
    defaultAAP.setProxyTargetClass(true);
    return defaultAAP;
  }

  @Bean
  public MethodInvokingFactoryBean methodInvokingFactoryBean(SecurityManager securityManager){
    MethodInvokingFactoryBean bean = new MethodInvokingFactoryBean();
    bean.setStaticMethod("org.apache.shiro.SecurityUtils.setSecurityManager");
    bean.setArguments(securityManager);
    return bean;
  }

  @ConfigurationProperties(prefix = "spring.redis")
  @Bean("shiroRedisManager")
  public RedisManager redisManager(){
    return new RedisManager();
  }

  @Bean("shiroRedisCacheManager")
  public RedisCacheManager cacheManager(RedisManager manager) {
    RedisCacheManager redisCacheManager = new RedisCacheManager();
    redisCacheManager.setRedisManager(manager);
    return redisCacheManager;
  }
    /*
    禁用SESSION，所以此配置无效
    @Bean
    public RedisSessionDAO redisSessionDAO(){
        RedisSessionDAO dao = new RedisSessionDAO();
        dao.setRedisManager(redisManager());
        return dao;
    }

    @Bean
    public DefaultWebSessionManager sessionManager() {
        DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
        sessionManager.setSessionValidationSchedulerEnabled(false);
        //sessionManager.setSessionDAO(redisSessionDAO());
        return sessionManager;
    }*/
}
